package com.itboye.eweilisten.util.widget;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.base.BaseDialogFragment;
import com.itboye.eweilisten.me.home.ActivityExperience;
import com.itboye.eweilisten.me.home.ActivityGoodsOrderActivity;
import com.itboye.eweilisten.model.GoodsDetailBean;
import com.itboye.eweilisten.util.loadingutil.MAlert;

import java.text.DecimalFormat;

/**
 * Created by itboye on 2017/3/11.
 */

public class ExperienceDialog extends BaseDialogFragment {
    private Context context;
    private TextView tvexperienceDialog;
    private TextView tvContext;
    private TextView tvlijigoumai;
    GoodsDetailBean bean;
    boolean falg;
    String str = "请免费体验后再下单购买!并请注意:此款英语听力微测试是在线服务产品,购买后不退不换!";
    String value_id;
    int post;
    double price;

    public ExperienceDialog() {

    }

    @SuppressLint("ValidFragment")
    public ExperienceDialog(Context context,GoodsDetailBean bean, boolean falg, String value_id, int post) {
        this.context=context;
        this.bean = bean;
        this.falg = falg;
        this.value_id = value_id;
        this.post = post;

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return LayoutInflater.from(getContext()).inflate(
                R.layout.experience_dialog, null);
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);
        onColik();
    }

    /**
     * 所有的点击事件
     */
    public void onColik() {
        SpannableStringBuilder style = new SpannableStringBuilder(str);
        style.setSpan(new ForegroundColorSpan(Color.RED), 34, style.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvContext.setText(style);
        if(value_id==null || value_id.equals("")){
            MAlert.alert("请选择期限");
            return;
        }else {
            tvexperienceDialog.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getContext(), ActivityExperience.class);
                    intent.putExtra("imgid", bean.getMain_img());
                    intent.putExtra("countment", bean.getSecondary_headlines());
                    intent.putExtra("name", bean.getName());
                    intent.putExtra("id", value_id);
                    startActivity(intent);
                    dismiss();
                }
            });
        }
        tvlijigoumai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (value_id == null || value_id.equals("")) {
                    MAlert.alert("请选择");
                }else if(!(bean.getExpire_time().equals("0")||bean.getExpire_time().equals(""))){
                    AlertDialog.Builder builder=new AlertDialog.Builder(context);
                    builder.setTitle("友情提示");
                    builder.setMessage("您已经购买该产品");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            startGoodsOrderActivity();
                        }
                    });
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                        }
                    });
                    builder.create().show();
                }else {
                    startGoodsOrderActivity();
                }
            }
        });


    }

    private void startGoodsOrderActivity() {
        Log.d("tvGoodsPrice",post+"");
        Log.d("===validtime===",bean.getExpire_time());
        price=Double.parseDouble(bean.getSku_list().get(post).getPrice());
        DecimalFormat df = new DecimalFormat("######0.00");
        String douPrice=  df.format(price/100);
        Intent intent = new Intent(getContext(), ActivityGoodsOrderActivity.class);
        intent.putExtra("countment", bean.getName());
        intent.putExtra("price", douPrice+"");
        // intent.putExtra("math", value_id);
        intent.putExtra("math", bean.getSku_list().get(post).getSku_desc());
        intent.putExtra("imgid", bean.getMain_img());
        intent.putExtra("id", bean.getId());
        intent.putExtra("sku_pkid",bean.getSku_list().get(post).getSku_pkid());
        startActivity(intent);
        dismiss();
    }

}
